import sys
sys.path.append("../reading_data")

import logging
from logging import debug
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)

import cPickle as pickle

import numpy as np
import matplotlib.pyplot as plt
import datetime
from matplotlib.dates import num2date
from matplotlib.dates import date2num


from read_from_h5 import get_temp_data, load_data

from metrics import pca_nipals
from numpy.lib import recfunctions


def get_clean_array(data, sel_tbs=None):
    all_tbs = data.dtype.names
    if not sel_tbs:
        sel_tbs = all_tbs
    arr_data = np.array([data[sel_tbs[0]]]).T
    for tb in sel_tbs[1:]:
        arr_data = np.append(arr_data, np.array([data[tb]]).T, 1)

    return arr_data


def find_time_and_turbines():
    p_file = open("../../bindata/severe_faults.pickle", 'rb')
    severe_faults = pickle.load(p_file)
    gear_changes = severe_faults[30]
    return gear_changes

def get_data(turbine, time, time_back=100):
    turbine = turbine.upper()
    start_time = num2date(time)
    prev_time = start_time - datetime.timedelta(time_back)
    h5_file = "../../bindata/data.h5"
    filt_strings = ["(time >= " + str(date2num(prev_time)) + " )",
                    #"(time <= " + str(date2num(start_time)) + " )"]
                    "(time <= " + str(date2num(start_time)) + " )",
                    "(activepoweravg > 100)",
                    "(activepoweravg < 1900)",
                    "(turbid == '" + turbine + "' )"]
    data = load_data(h5_file, filt=filt_strings)
    return data


def get_pca_fault_model():
    look_here = find_time_and_turbines()
    temp_sets = []
    turbids = []

    for turbine, time in look_here[0:10]:
        data = get_data(turbine, time)
        temp_data = get_temp_data(data)
        temp_data = recfunctions.drop_fields(temp_data, "generatorbeartempavg")
        mdat = get_clean_array(temp_data)
        mdat = np.copy(mdat[~np.isnan(mdat).any(1)])
        temp_sets.append(mdat)
        turbids.append(turbine)

    tt, pp, pr, eigs = pca_nipals(temp_sets[0], 17)
    mean = np.mean(temp_sets[0], axis=0)

    fig, ax = plt.subplots(1,1)
    ax.plot(tt[:,0], tt[:,1], 'o', label=turbids[0])
    for temp_set, turbid in zip(temp_sets[1:], turbids[1:]):
        t1 = np.dot((temp_set - mean), pp.T)
        ax.plot(t1[:,0], t1[:,1], 'o', label=turbid, markersize=5, alpha=0.7)
    ax.legend()

    fig, ax = plt.subplots(1, 1)
    ax.plot(tt[:, 1], tt[:, 2], 'o', label=turbids[0])
    for temp_set, turbid in zip(temp_sets[1:], turbids[1:]):
        t1 = np.dot((temp_set - mean), pp.T)
        ax.plot(t1[:,1], t1[:,2], 'o', label=turbid, markersize=5, alpha=0.7)
    ax.legend()
    return pp, mean

def main():
    """
    >>> main()
    """
    pp, mean = get_pca_fault_model()
    #run_turb_on_model(pp, mean)
    plt.show()
    return

def run_turb_on_model(pp, mean):
    turbines = ["WH11" + str(j) + str(i) for j in range(0,2,1) for i in range(1,9,1)]
    turbines.extend(["WH12" + str(j) + str(i) for j in range(2,3,1) for i in range(1,9,1)])
    time = date2num(datetime.datetime(2009, 07, 01))
    temp_sets = []
    turbids = []

    for turbine in turbines[0:3]:
        data = get_data(turbine, time, 20)
        temp_data = get_temp_data(data)
        temp_data = recfunctions.drop_fields(temp_data, "generatorbeartempavg")
        mdat = get_clean_array(temp_data)
        mdat = np.copy(mdat[~np.isnan(mdat).any(1)])
        temp_sets.append(mdat)
        turbids.append(turbine)


    fig, ax = plt.subplots(1,1)
    for temp_set, turbid in zip(temp_sets[0:], turbids[0:]):
        t1 = np.dot((temp_set - mean), pp.T)
        ax.plot(t1[:,0], t1[:,1], 'o', label=turbid, markersize=5, alpha=0.7)
    ax.legend()

    fig, ax = plt.subplots(1, 1)
    for temp_set, turbid in zip(temp_sets[0:], turbids[0:]):
        t1 = np.dot((temp_set - mean), pp.T)
        ax.plot(t1[:,1], t1[:,2], 'o', label=turbid, markersize=5, alpha=0.7)
    ax.legend()
    return

def pca_temp_model():
    """
    #>>> pca_temp_model()
    """
    h5_file = "../../bindata/wh1.h5"
    #start_days = [str(date2num(datetime.datetime(2011, i+1, 1))) for i in range(10)]
    #stop_days = [str(date2num(datetime.datetime(2011, i+2, 1))) for i in range(10)]
    #start_day = start_days[0]
    #stop_day = stop_days[-1]
    turbines = ["WH11" + str(j) + str(i) for j in range(0,2,1) for i in range(1,9,1)]
    turbines.extend(["WH12" + str(j) + str(i) for j in range(2,3,1) for i in range(1,9,1)])
    debug(turbines)
    temp_sets = []
    turbids = []
    for turbid in turbines[0:7]:
        filt_strings = ["(activepoweravg > 200)",
                        "(activepoweravg < 1900)",
                        "(turbid == '" + turbid + "' )",
                        "(time >= " + str(date2num(datetime.datetime(2011,01,01))) + " )",
                        "(time <= " + str(date2num(datetime.datetime(2011,02,01))) + " )"]
        data = load_data(h5_file, filt=filt_strings)
        temp_data = get_temp_data(data)
        temp_data = recfunctions.drop_fields(temp_data, "generatorbeartempavg")
        mdat = get_clean_array(temp_data)
        mdat = np.copy(mdat[~np.isnan(mdat).any(1)])
        temp_sets.append(mdat)
        turbids.append(turbid)

    tt, pp, pr, eigs = pca_nipals(temp_sets[0], 17)
    mean = np.mean(temp_sets[0], axis=0)

    # pr is how many procent of the data that are explained by the components
    debug(pr)
    #debug(pp)
    debug(eigs.shape)
    debug(tt.shape)
    debug(pr.shape)
    debug(pp.shape)
    fig, ax = plt.subplots(1,1)
    ax.plot(tt[:,0], tt[:,1], 'o', label=turbids[0])
    for temp_set, turbid in zip(temp_sets[1:], turbids[1:]):
        t1 = np.dot((temp_set - mean), pp.T)
        ax.plot(t1[:,0], t1[:,1], 'o', label=turbid, markersize=5, alpha=0.7)
    ax.legend()

    fig, ax = plt.subplots(1, 1)
    ax.plot(tt[:, 1], tt[:, 2], 'o', label=turbids[0])
    for temp_set, turbid in zip(temp_sets[1:], turbids[1:]):
        t1 = np.dot((temp_set - mean), pp.T)
        ax.plot(t1[:,1], t1[:,2], 'o', label=turbid, markersize=5, alpha=0.7)
    ax.legend()



    plt.show()
    return
    debug(mean)
    #debug(tt)
    #debug(tt.shape)
    #debug(len(tt[:,1]))
    #debug(len(data['time']))
    return
    temp_data = np.ma.array(temp_data, mask)
    debug(type(data))
    new_temp_data = np.ma.compress(np.ma.fix_invalid(temp_data))
    debug(new_temp_data)
    return
    fig, ax = plt.subplots(1,1)
    ax.plot(num2date(data['time']), temp_data["generatorbear2tempavg"], 'o',  markersize=2)
    fig.autofmt_xdate()
    plt.show()
    return
    plt.plot(temp_data)
    pca_nipals(temp_data, 10)
    return



if __name__=='__main__':
    import pca_model
    import doctest
    doctest.testmod(pca_model, verbose=False)


"""
    debug(type(temp_data))

    fig, ax = plt.subplots(1,1)

    fig.autofmt_xdate()
    for dtype in temp_data.dtype.names[0:2]:
        debug(dtype)
        plt.plot(num2date(data['time']), temp_data[dtype], 'o')

    plt.show()
    return
    ax.plot(num2date(data['time']), data['gearbeartempavg'], 'o')
    fig.autofmt_xdate()
    plt.show()
    return
"""
